// 队列 FIFO

class Queue {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.items = {}
    }

    enqueue(el) {
        // 向队列尾部(*)添加一个或多个项
        this.items[this.count] = el;
        this.count++
    }

    dequeue() {
        // 删除第一项
        if (this.isEmpty()) {
            return;
        }
        const res = this.items[this.lowestCount];
        delete this.items[this.lowestCount];
        this.lowestCount++;
        return res;
    }

    peek() {
        // 返回第一项
        // 只返回信息，不操作数据
        if (this.isEmpty()) {
            return;
        }
        return this.items[this.lowestCount]
    }

    isEmpty() {
        // 是否为空
        return this.size() === 0
    }

    size() {
        // 队列长度
        return this.count - this.lowestCount;
    }

    clear() {
        this.items = {};
        this.count = 0;
        this.lowestCount = 0;
    }

    toString() {
        if (this.isEmpty()) {
            return '';
        }
        let objString = `${this.items[this.lowestCount]}`;
        for (let i = this.lowestCount + 1; i < this.count; i++) {
            objString = `${objString}, ${this.items[i]}`
        }
        return objString
    }
}